<html>
<head>
  <title>05numpy</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="565"/>
<h1>05numpy</h1>

<div>
<span><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">基本功能</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">快速高效的多维数组对象ndarray</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">用于对数组执行元素级计算以及直接对数组执行数学运算的函数</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">用于读写硬盘上基于数组的数据集的工具</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">线性代数运算、傅里叶变换，以及随机数生成</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">用于将C、C++、Fortran代码集成到Python的工具</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">除了为Python提供快速的数组处理能力，NumPy在数据分析方面还有另外一</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">个主要作用，即作为在算法之间传递数据的容器。</span></div><div><br/></div><div><br/></div><div><br/></div><div><span style="font-size: 19px;"><span style="font-size: 19px; color: rgb(28, 51, 135); font-weight: bold;">define our features and labels</span></span> ：</div><div><br/></div><div>X = np.array(df.drop(['label'], 1))</div><div>y = np.array(df['label'])</div><div><span style="font-size: 19px; color: rgb(28, 51, 135); font-weight: bold;">Numpy之文件存取：</span></div><div>Numpy可以将数组保存至二进制文件、文本文件，同时支持将多个数组保存至一个文件中。</div><div>1. np.tofile() &amp; np.fromfile()</div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">import numpy as np</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">import os</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">os.chdir(&quot;d:\\&quot;)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">a = np.arange(0,12)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">a.reshape(3,4)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">array([[ 0,  1,  2,  3],</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    [ 4,  5,  6,  7],</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    [ 8,  9, 10, 11]])</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">a.tofile(&quot;a.bin&quot;)                     #保存至a.bin</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">b = np.fromfile(&quot;a.bin&quot;, dtype=np.int32)  #从文件中加载数组，错误的dtype会导致错误的结果</span></div><div><br/></div></div><div> 2. np.save() &amp; np.load() &amp; np.savez()</div><div>     load()和save()用Numpy专用的二进制格式保存数据，它们会自动处理元素类型和形状等信息。savez()提供了将多个数组存储至一个文件的能力，调用load()方法返回的对象，可以使用数组名对各个数组进行读取。</div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">np.save(&quot;a.npy&quot;, a.reshape(3,4))</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">c = np.load(&quot;a.npy&quot;)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">c</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">array([[ 0,  1,  2,  3],</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">       [ 4,  5,  6,  7],</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">       [ 8,  9, 10, 11]])</span></div></div><div> 3. savetxt() &amp; loadtxt()</div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">数组创建函数</span></font></div><div><img src="05numpy_files/Image.png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import numpy as np</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('使用普通一维数组生成Numpy一维数组')</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">data = [6,7.5,8,0,1]</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">arr = np.array(data)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('打印元素类型')</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr.dtype)</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">使用普通二维数组生成NumPy二维数组</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">data = [[1, 2, 3, 4], [5, 6, 7, 8]]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.array(data)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(arr)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 9pt;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">打印数组维度</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 9pt;">')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(arr.shape)</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">print('使用zeros/empty')</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">print(np.zeros(10) # </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">生成包含10个0的一维数组</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(np.zeros((3, 6)) # 生成3*6的二维数组</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(np.empty((2, 3, 2))<span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 生成2*3*2的三维数组，所有元素未初始化。</span></span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('使用arrange生成连续元素')</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(np.arange(15))    </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># [0, 1, 2, ..., 14]</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">NumPy的ndarray NumPy数据类型</span></font></div><div><img src="05numpy_files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><img src="05numpy_files/Image [2].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">ndarray 数组和标量之间的运算</span></font></div><div>• 不用编写循环即可对数据执行批量运算</div><div>• 大小相等的数组之间的任何算术运算都会将运算应用到元素级</div><div>• 数组与标量的算术运算也会将那个标量值传播到各个元素</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div># -*- coding: utf-8 -*-</div><div><br/></div><div>import numpy as np</div><div><br/></div><div><span style="font-size: 9pt;"># 数组乘法／减法，对应元素相乘／相减。</span></div><div><span style="font-size: 9pt;">arr = np.array([[1.0, 2.0, 3.0], [4., 5., 6.]])</span></div><div><span style="font-size: 9pt;">print (arr * arr)</span></div><div><span style="font-size: 9pt;">print(arr - arr)</span></div><div><span style="font-size: 9pt;"><br/></span></div><div><span style="font-size: 9pt;"># 标量操作作用在数组的每个元素上</span></div><div><span style="font-size: 9pt;">arr = np.array([[1.0, 2.0, 3.0], [4., 5., 6.]])</span></div><div><span style="font-size: 9pt;">print 1 / arr</span></div><div><span style="font-size: 9pt;">print arr ** 0.5  # 开根号</span></div></div><div><img src="05numpy_files/Image [3].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>ndarray 基本的索引和切片</b></font></span></div><div><br/></div><div><span>•</span> <span>索引原理</span></div><div><span>•</span> <span>切片原理</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>import numpy as np</div><div><br/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 通过索引访问二维数组某一行或某个元素</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print(arr[2])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print(arr[0][2])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print(arr[0,2])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print()</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 对更高维数组的访问和操作</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.array([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print(arr[0]) <span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 结果是个2维数组</span></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print(arr[1, 0]) <span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 结果是个2维数组</span></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">out_values = arr[0].copy() <span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 复制arr[0]的值</span></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr[0] = 42 <span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 把arr[0]所有的元素都设置为同一个值</span></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print(arr)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print()</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print ('使用切片访问和操作数组')</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (arr[1:6])  # 打印元素arr[1]到arr[5]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (arr[:2]) # 打印第1、2行</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (arr[:2, 1:]) # 打印第1、2行，第2、3列</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (arr[:, :1])  # 打印第一列的所有元素</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr[:2, 1:] = 0 # 第1、2行，第2、3列的元素设置为0</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (arr)</span></div></div><div><img src="05numpy_files/Image [4].png" type="image/png" data-filename="Image.png"/>          <img src="05numpy_files/Image [5].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>ndarray 布尔型索引</b></font></span></div><div><span>•</span> <span>布尔型数组的长度必须跟被索引的轴长度一致。</span></div><div><span>•</span> <span>可以将布尔型数组跟切片、整数（或整数序列）混合使用</span><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy as np</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy.random as np_random</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print '使用布尔数组作为索引'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">name_arr = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">rnd_arr = np_random.randn(7, 4) # 随机7*4数组</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (rnd_arr)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (name_arr == 'Bob') # 返回布尔数组，元素等于'Bob'为True，否则False。</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (rnd_arr[name_arr == 'Bob'])  # 利用布尔数组选择行</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (rnd_arr[name_arr == 'Bob', :2])  # 增加限制打印列的范围</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (rnd_arr[-(name_arr == 'Bob')]) # 对布尔数组的内容取反</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">mask_arr = (name_arr == 'Bob') | (name_arr == 'Will') # 逻辑运算混合结果</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (rnd_arr[mask_arr])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">rnd_arr[name_arr != 'Joe'] = 7  # 先布尔数组选择行，然后把每行的元素设置为7。</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (rnd_arr)</span></div><div><br/></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>ndarray 花式索引</b></font></span></div><div><span>•</span> <span>花式索引（Fancy indexing）是一个NumPy术语，它指的是利用整数数组进</span></div><div><span>行索引。</span></div><div><span>•</span> <span>一次传入多个索引数组会有一点特别。它返回的是一个一维数组，其中的元素</span></div><div><span>对应各个索引元组。</span><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy as np</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print 'Fancy Indexing: 使用整数数组作为索引'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.empty((8, 4))</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">for i in range(8):</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">    arr[i] = i</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr[[4, 3, 0, 6]] # 打印arr[4]、arr[3]、arr[0]和arr[6]。</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr[[-3, -5, -7]] # 打印arr[3]、arr[5]和arr[-7]行</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.arange(32).reshape((8, 4))  # 通过reshape变换成二维数组</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr[[1, 5, 7, 2], [0, 3, 1, 2]] # 打印arr[1, 0]、arr[5, 3]，arr[7, 1]和arr[2, 2]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]  # 1572行的0312列</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])] # 可读性更好的写法</span></div></div><div><br/></div><div><span><br/></span></div><div><span><br/></span></div><div><span><br/></span></div><div><span><br/></span></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>ndarray 数组转置和轴对换</b></font></span></div><div><span>•</span> <span>一维／二维数组转置</span></div><div><span>•</span> <span>高维数组轴对换</span><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy as np</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy.random as np_random</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print '转置矩阵'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.arange(15).reshape((3, 5))</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.T</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print '转置矩阵做点积'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np_random.randn(6, 3)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print np.dot(arr.T, arr)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print '高维矩阵转换'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.arange(16).reshape((2, 2, 4))</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">'''</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">详细解释：</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr数组的内容为</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a[0][0] = [0, 1, 2, 3]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a[0][1] = [4, 5, 6, 7]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a[1][0] = [8, 9, 10, 11]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a[1][1] = [12, 13, 14, 15]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">transpose的参数为坐标，正常顺序为(0, 1, 2, ... , n - 1)，</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">现在传入的为(1, 0, 2)代表a[x][y][z] = a[y][x][z]，第0个和第1个坐标互换。</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a'[0][0] = a[0][0] = [0, 1, 2, 3]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a'[0][1] = a[1][0] = [8, 9, 10, 11]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a'[1][0] = a[0][1] = [4, 5, 6, 7]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">- a'[1][1] = a[1][1] = [12, 13, 14, 15]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">'''</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.transpose((1, 0, 2))</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.swapaxes(1, 2)  # 直接交换第1和第2个坐标</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>ndarray 快速的元素级数组函数</b></font></span></div><div><ul><li><font style="font-size: 14pt;"><b>一元函数</b></font></li></ul></div><div><img src="05numpy_files/Image [6].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><img src="05numpy_files/Image [7].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><ul><li><span style="line-height: 1.45;">二元函数</span><br/></li></ul><div><img src="05numpy_files/Image [8].png" type="image/png" data-filename="Image.png"/></div></div><div><br/></div><div><img src="05numpy_files/Image [9].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></span>
</div></body></html> 